# -*- coding: cp936 -*-
# Create your views here.
from django.http import HttpResponse
from weibopy.auth import OAuthHandler
from doubancon.models import Douban_Access_Token
from myuser.models import MyUser

from myuser.usercheck import getUser

from google.appengine.api import users

import logging
from django.shortcuts import render_to_response,redirect,render_to_response
import simplejson

def home(request):
    """
    """
    #return HttpResponse(simplejson.dumps(request.COOKIES))
    dic = {}
    dic['debuginfo'] = (simplejson.dumps(request.COOKIES))
    user = getUser(request)
    if user == None:
        #return HttpResponse("no cookie")
        return render_to_response('hola/index.html',dic)
    else:
        dic['user'] = user
        dic['username'] = user.nickname
        return render_to_response("hola/index.html",dic)

    #check username and pwd
    if request.COOKIES.has_key("username") and request.COOKIES.has_key("pwd"):
        return None
    dic = {}
    dic['googleloginurl'] = users.create_login_url("/")
    try:
        at = request.COOKIES['sinaaccesstoken']
    except:
        at = ""

    try:
        dat = request.COOKIES['doubanaccesstoken']
    except:
        dat = ""
        
    if at != "":
        oah = OAuthHandler("1330786948","94fa907c07c9d6915927f48149f431d8")
        atsec = Access_Token.all().filter("thekey = ",at)[0].secret
        oah.set_access_token(at,atsec)

        #return HttpResponse(ot+":"+os+":"+ov)
    
        return HttpResponse(u"username:"+oah.get_username())
    else:
        return render_to_response('hola/index.html',dic)
